import wx
import time
import math

windowWidth = 800
windowHeight = 1000

class MainWindow(wx.Frame):
	def __init__(self, parent, id, title):
		wx.Frame.__init__(self, parent, wx.ID_ANY, title, size = (windowWidth, windowHeight), style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
		panel = DrawPanel(self)
		#textBox = TextPanel(self)
		hbox = wx.BoxSizer(wx.HORIZONTAL)
		self.tc = wx.TextCtrl(panel, -1, size = (250, 952), style = wx.TE_MULTILINE )
		hbox.Add(self.tc, 0 , wx.RIGHT)
		panel.SetSizer(hbox)
		
		self.runningTime = 0
		self.showTime = 0
		self.SetSizeHints(windowWidth, windowHeight, windowWidth, windowHeight, incW=-1, incH=-1)
		
		# Draw a status bar
		self.statusbar = self.CreateStatusBar()
		self.statusbar.SetFieldsCount(3)
		self.statusbar.SetStatusWidths([-2, -3, -5])
				
		self.timer = wx.PyTimer(self.StatusBarShowTime)
		self.timer.Start(1000)
		self.StatusBarShowTime()

		self.Centre()
		self.Show(True)
		
	def StatusBarShowTime(self): # Update the string on the status bar ... from above it does this once per second
		""" Shows the time in the bottom right status bar column 2 of [0|1|2] """
		self.runningTime += 1
		self.SetStatusText("%s sec" % (str(self.runningTime)),0) # third position on the status bar

		
class DrawPanel(wx.Panel):
	def __init__(self, parent):
		self.timer = wx.PyTimer(self.OnTimerPaint)
		self.timer.Start(1000)
		self.MouseClicks = 0
		wx.Panel.__init__(self, parent, -1)
		self.circlePoints = []
		self.newCirclePoints = []
		self.OMD_yPosStart = 0
		self.OMD_xPos = 0
		wx.EVT_PAINT(self, self.OnPaint)
		wx.EVT_LEFT_DOWN (self, self.OnMouseDown)
		wx.EVT_LEFT_UP(self, self.OnMouseUp_Physics)
#		wx.EVT_TIMER(self, self.OnTimerPaint)

	def OnTimerPaint(self, event):
		self.OnPaint(self)
		
	def OnPaint(self, event):
		self.font = wx.Font(8,wx.FONTFAMILY_MODERN,wx.FONTSTYLE_NORMAL,wx.FONTWEIGHT_LIGHT)
		dc = wx.PaintDC(self)
		dc.SetFont(self.font)
		dc.Clear()
		dc.SetPen(wx.Pen("#000000", 1))
		lcv = 0
		for point in self.circlePoints:
				dc.DrawEllipse(point.x-5, point.y-5, 10, 10)
				dc.DrawText("(" + str(point.x) + "," + str(point.y) + ")", point.x+5, point.y-20)
				lcv +=1
		for points in self.newCirclePoints:
				dc.DrawEllipse(points[0]-5, points[1]-5, 10, 10)

	def OnMouseDown(self, event):
		textbox = self.GetParent().tc
		self.MouseClicks += 1
		point = event.GetPosition()
		self.OMD_yPosStart = event.GetY()
		self.OMD_xPos = event.GetX()
		statusbar = self.GetParent().statusbar
		statusbar.SetStatusText(str(self.MouseClicks) + " objects", 0)
		statusbar.SetStatusText("Pos = " + str(point), 1)
		textbox.AppendText("\n---- Circle " + str(self.MouseClicks) + " Pos = " + str(point) + " ----\n")
		textbox.AppendText("Time (s) \t Distance from Bottom (m)\n")
		self.circlePoints = self.circlePoints + [point]
		
		self.Refresh()
	def OnMouseUp_Physics(self, event):
		timeDelta = 0
		statusbar = self.GetParent().statusbar
		textbox = self.GetParent().tc
		self.yStart = self.OMD_yPosStart
		self.y = self.yStart
		self.x = self.OMD_xPos
		#statusbar.SetStatusText(str(self.circlePoints[self.MouseClicks - 2]), 2)
		#timeStart = self.GetParent().runningTime
		#timeDelta = timeStart
		boundary = windowHeight-48-2
		while 0.5*(9.8)*(timeDelta)*(timeDelta) <= (boundary) - self.yStart: #48 for the status bar, 2 guess for no collision of ball with statusbar
			self.y = self.yStart + 0.5*(9.8)*(timeDelta)**2
			points = (self.x, self.y)
			self.newCirclePoints = self.newCirclePoints + [points]
			timeDelta += .075
			statusbar.SetStatusText(str(timeDelta) +", (" + str(boundary  - self.newCirclePoints[-1][1]) + ")", 2)
			textbox.AppendText(str(timeDelta) + "\t(" + str(boundary  - self.newCirclePoints[-1][1]) + ")\n")
	
#	def updateCircle(self, self.GetParent().timer)
#		self.freeFallEquation(

app = wx.App(0)
frame = MainWindow(None, -1, "Draw")
app.MainLoop()